import VueRouter from 'vue-router'
import Vue from 'vue'
Vue.use(VueRouter)
import login from '@/views/login/login.vue'
import register from '@/views/login/register.vue'
import layout from '@/views/layout/layout.vue'
import chart from '@/views/layout/chart/chart.vue'
import userList from '@/views/layout/userList/userList.vue'
import question from '@/views/layout/question/question.vue'
import business from '@/views/layout/business/business.vue'
import subject from '@/views/layout/subject/subject.vue'
import addSubject from "@/views/layout/subject/addSubject.vue";

//解决在路由中添加了相同的路由的bug
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: login,
            meta: {
                title: '登录'
            }
        },
        {
            path: '/login',
            component: login,
            meta: {
                title: '登录'
            }
        },
        {
            path: '/register',
            component: register,
            meta: {
                title: '注册'
            }
        },
        {
            path: '/layout',
            component: layout,
            redirect: '/layout/chart',
            children: [
                {
                    path: 'chart',
                    component: chart,
                    meta: {
                        title: '数据概览'
                    }
                },
                {
                    path: 'userList',
                    component: userList,
                    meta: {
                        title: '用户列表'
                    }
                },
                {
                    path: 'question',
                    component: question,
                    meta: {
                        title: '题库列表'
                    }
                },
                {
                    path: 'business',
                    component: business,
                    meta: {
                        title: '企业列表'
                    }
                },
                {
                    path: 'subject',
                    component: subject,
                    meta: {
                        title: '学科列表'
                    }
                },
                {
                    path: '/subject/addSubject',
                    component: addSubject
                }
            ]
        }
    ]
})

//设置页面跳转的进度条
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
router.beforeEach((to, from, next) => {
    nprogress.start();
    next()
})
router.afterEach((to) => {
    document.title = to.meta.title
    nprogress.done()
})

export default router